import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueRequestor;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * 
 */

/**
 * @author mpriet
 *
 */
public class Request extends Thread {

	private String  queueName = "comparticion.service";
	@SuppressWarnings("unused")
	private int     exitResult = 0;
	
	public Request() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param target
	 */
	public Request(Runnable target) {
		super(target);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param name
	 */
	public Request(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param group
	 * @param target
	 */
	public Request(ThreadGroup group, Runnable target) {
		super(group, target);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param group
	 * @param name
	 */
	public Request(ThreadGroup group, String name) {
		super(group, name);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param target
	 * @param name
	 */
	public Request(Runnable target, String name) {
		super(target, name);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param group
	 * @param target
	 * @param name
	 */
	public Request(ThreadGroup group, Runnable target, String name) {
		super(group, target, name);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param group
	 * @param target
	 * @param name
	 * @param stackSize
	 */
	public Request(ThreadGroup group, Runnable target, String name,
			long stackSize) {
		super(group, target, name, stackSize);
		// TODO Auto-generated constructor stub
	}
	
	public void run() {
        QueueConnectionFactory    queueConnectionFactory = null;
        QueueConnection           queueConnection = null;
        QueueSession              queueSession = null;
        Queue                     queue = null;
        QueueRequestor            queueRequestor = null;
        TextMessage               message = null;
        final String              MSG_TEXT = new String("Este es la request");
        TextMessage               reply = null;
        String                    replyID = null;

        try {
            queueConnectionFactory = 
                SampleUtilities.getQueueConnectionFactory();
            queueConnection = 
                queueConnectionFactory.createQueueConnection();
            queueSession = queueConnection.createQueueSession(false, 
                Session.AUTO_ACKNOWLEDGE);
            queue = SampleUtilities.getQueue(queueName, (Session) queueSession);
        } catch (Exception e) {
            System.out.println("Connection problem: " + e.toString());
            if (queueConnection != null) {
                try {
                    queueConnection.close();
                } catch (JMSException ee) {}
            }
            System.exit(1);
        } 

        /*  
         * Create a QueueRequestor.
         * Create a text message and set its text.
         * Start delivery of incoming messages.
         * Send the text message as the argument to the request method, 
         * which returns the reply message.  The request method also
         * creates a temporary queue and places it in the JMSReplyTo
         * message header field.
         * Extract and display the reply message.
         * Read the JMSCorrelationID of the reply message and confirm that 
         * it matches the JMSMessageID of the message that was sent.
         * Finally, close the connection.
         */
        try {
            queueRequestor = new QueueRequestor(queueSession, queue);
            message = queueSession.createTextMessage();
            message.setText(MSG_TEXT);
            System.out.println("REQUEST: Sending message: " 
                + message.getText());
            queueConnection.start();
            reply = (TextMessage) queueRequestor.request(message);
            System.out.println("REQUEST: Reply received: " 
                + reply.getText());
           if(reply.getJMSCorrelationID()!=null){
                replyID = new String(reply.getJMSCorrelationID());
                if (replyID.equals(message.getJMSMessageID())) {
                    System.out.println("REQUEST: OK: Reply matches sent message");
                } else {
                    System.out.println("REQUEST: ERROR: Reply does not match sent message");
                }
           }
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
            exitResult = 1;
        } catch (Exception ee) {
            System.out.println("Unexpected exception: " + ee.toString());
            ee.printStackTrace();
            exitResult = 1;
        } finally {
            if (queueConnection != null) {
                try {
                    queueConnection.close();
                } catch (JMSException e) {
                    exitResult = 1;
                }
            }
        }
    }

}
